package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author 杨旭康
 * @Date 2022/11/2 11:50
 * @question:
 */

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * 请求 URL: http://localhost:8080/category
     * 请求方法: POST
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {

        categoryService.save(category);

        return R.success("新增分类成功");
    }

    /**
     * 分类信息条件查询
     * 请求 URL: http://localhost:8080/category/page?page=1&pageSize=10
     * 请求方法: GET
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> selectPage(int page,int pageSize) {

        Page<Category> pageInfo = categoryService.selectPage(page, pageSize);

        return R.success(pageInfo);
    }

    /**
     * 根据id删除分类
     * 请求 URL: http://localhost:8080/category?id=1413386191767674881
     * 请求方法: DELETE
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> removeById(Long id) {

        categoryService.removeId(id);
        return R.success("分类删除成功~");
    }

    /**
     * 修改分类方法===>>回显已在前端完成 直接修改即可
     * 请求 URL: http://localhost:8080/category
     * 请求方法: PUT
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {

        log.info("修改分类 : {}",category);
        categoryService.updateById(category);

        return R.success("修改分类成功");
    }

    /**
     * 关于  菜品和分类  下拉分类列表的展示
     * 请求 URL: http://localhost:8080/category/list?type=1
     * 请求方法: GET
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> listR(Category category) {

        List<Category> categories = categoryService.listR(category);

        return R.success(categories);
    }




}
